<?php
class personalMod extends commonMod{
	
	public function __construct(){
		parent::__construct();
	}

	public function index(){
		if(isset($_GET['uid'])){
			$uid = intval($_GET['uid']);
		}else{
			$uid = $_SESSION['uid'];
		}		
		$condition = array('uid'=>$uid);
		$result = $this->model->table('staff_detail')->where($condition)->find();
		//$this->assign('cat', $this->getCat(14));
		if($result==FALSE){
			//false 表示没有完善个人信息
			$this->assign('cat',$this->getCat());
			$this->display('personal/compelete');
		}else{
			//已完善个人信息后数据查询并输出
			$rightCat = parent::getRightCat($result['department']);
			$aresult = $this->model->table('accessory')->where($condition)->select();
			//format date 格式化时间格式
			$result['birthday'] = date('Y-m-d',$result['birthday']);
			$result['ruzhitime'] = date('Y-m-d',$result['ruzhitime']);
			$result['lizhitime'] = date('Y-m-d',$result['lizhitime']);
			$this->assign('rightCat', $rightCat);			
			$this->assign('result', $result);
			$this->assign('aresult', $aresult);
			$this->display('personal/personalInfo');
		}
		
	}
	
	public function changeNP(){
		//修改name和password
		$this->display();
	}
	
	public function updateNP(){
		//N name P password
		if(!empty($_POST['newname'])){
			$progess         .= 'hasName';
			$oldname          = in($_POST['oldname']);
			$data['username'] = in($_POST['newname']);
		}
		if(!empty($_POST['newpass1']) || !empty($_POST['newpass2'])){
			if($_POST['newpass1']!=$_POST['newpass2']){
				$this->error('两次输入的密码不一样！');
			}else{
				$progess         .= 'hasPass';
				$oldpass          = md5($_POST['oldpass']);
				$data['password'] = in($_POST['newpass1']);
				$newpass2         = in($_POST['newpass2']);
			}	
		}
		if($data['username']=="" && $data['password']=="" && $newpass2==""){
			$this->error('没有要修改的内容！');
			exit();
		}
		//echo $progess;
		switch($progess){
			case 'hasName':
				$result = $this->model->table('admin')->where('`username`='.$oldname)->count();
				if($result!=0){
					$upResult = $this->model->table('admin')->data($data)->where('`id`='.$_SESSION['uid'])->update();
				}else{
					$this->error('原用户名不匹配！');
				}
			break;
			case 'hasPass':
				$result = $this->model->table('admin')->where('`password`='.'"'.$oldpass.'"')->count();
				if($result!=0){
					$upResult = $this->model->table('admin')->data($data)->where('`id`='.$_SESSION['uid'])->update();
				}else{
					$this->error('原密码不匹配！');
				}
			break;
			case 'hasNamehasPass':
				$result = $this->model->table('admin')->where('`password`='.$oldpass.'`username`='.$oldname)->count();
				if($result!=0){
					$upResult = $this->model->table('admin')->data($data)->where('`id`='.$_SESSION['uid'])->update();
				}else{
					$this->error('原用户名或者密码不匹配！');
				}
			break;
			default:
				$this->error('未知错误！');
		}
	}
	
	public function compelete(){
		//header("Content-type: text/html; charset=utf-8");
		$pdata['name']            = in($_POST['username']);//$pdata means personalData
		$pdata['sex']             = in($_POST['sex']);
		$pdata['isMarry']         = in($_POST['isMarry']);
		$pdata['birthday']        = exchangeDate($_POST['birthday']);		
		$pdata['department']      = in($_POST['department']);
		$pdata['xueli']           = in($_POST['xueli']);
		$pdata['shenfenzhengNum'] = in($_POST['shenfenzhengNum']);
		$pdata['hukoulocation']   = in($_POST['hukoulocation']);
		$pdata['addr']            = in($_POST['addr']);
		$pdata['ruzhitime']       = exchangeDate($_POST['ruzhitime']);
		$pdata['lizhitime']       = exchangeDate($_POST['lizhitime']);
		$pdata['salary']          = in($_POST['salary']);
		$pdata['tel1']            = in($_POST['tel1']);
		$pdata['tel2']            = in($_POST['tel2']);
		$pdata['mobile']          = in($_POST['mobile']);
		$pdata['qq']              = in($_POST['qq']);
		$pdata['msn']             = in($_POST['msn']);
		$pdata['email']           = in($_POST['email']);
		$pdata['jianli']          = in($_POST['jianli']);
		$pdata['family']          = in($_POST['family']);	
		$pdata['uid']             = $_SESSION['uid'];
		$photoInfo = $this->_upload('menbersPhoto');//设置上传路径
		//dump($photoInfo);
		$dbUrl = '/upload/menbersPhoto/';
		$pdata['photo'] = $dbUrl.$photoInfo[0]['savename'];//组装存入数据库的路径
		if(count($photoInfo)>1){
			$adata['uid'] = $pdata['uid'];////$adata means accessoryData
			foreach ( $photoInfo as $key=>$value ){
				if($key==0) continue;
				$adata['accessoryPath'] = $dbUrl.$photoInfo[$key]['savename'];
				$inertInfo = $this->model->table('accessory')->data($adata)->insert();
				if($inertInfo==false){
					$errorInfo[$key] = $key; //check the fail loop 检查哪一次附件上传失败失败
				}
			}
		}
		$presult = $this->model->table('staff_detail')->data($pdata)->insert();
		//$aresult = count($errorInfo);
		if($presult){
			if(count($errorInfo)!=0){
				$this->success("个人信息更新成功。但是附件上传出现失败！");
			}else{
				$this->success('操作成功！');
			}
		}else{
			if(count($errorInfo)!=0){
				$this->error("个人信息添加失败。");
			}else{
				$this->error('个人信息添加失败！但是附件上传成功');
			}
		}
	}
	
	
}

?>